import resource from '../../../lib/resource';
import city from '../../../lib/city';
import tips from '../../../lib/tips';
var db = wx.cloud.database().collection('user');
Page({
  data: {
    loading: true,
    consignee: '',
    mobile: '',
    country: '',
    address: '',
    addressid: '',
    isDefault: false,
    items: {
      labelText: '设置为默认',
      iconType: 'circle',

    },
    index: 0,
    tipsData: {
      title: '',
      isHidden: true
    }
  },

  checkboxChange: function (e) {
    if (e.detail.value.length == 0)
      this.setData({
        isDefault: false
      });
    else
      this.setData({
        isDefault: true
      });
  },
  onLoad(options) {
    this.setData({
      addressid: options.id
    });
    var that = this;

    if (options.id) {
      resource.fetchDetailAddress(options.id).then((res) => {

        this.setData({
          consignee: res.data.consignee,
          mobile: res.data.mobile,
          county: res.data.county,
          province: res.data.province,
          city: res.data.city,
          address: res.data.address,
          loading: false,
          items: this.data.items
        });
        //this.setDefault();
        city.init(that);
      });
    } else {
      city.init(that);
    }

  },
  listenerReciverInput(e) {
    this.data.consignee = e.detail.value;
  },
  listenerPhoneInput(e) {
    this.data.mobile = e.detail.value;
  },
  listenerAddressInput(e) {
    this.data.address = e.detail.value;
  },

  submitBtn() {

    const that = this;
    if (!this.data.consignee) {
      wx.showToast({
        title: '收货人不能为空',
        icon: 'none',
        duration: 2000
      })
      return;
    }

    if (!this.data.mobile) {
      wx.showToast({
        title: '手机号不能为空',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (!/^1[3|4|5|7|8]\d{9}$/.test(this.data.mobile)) {
      wx.showToast({
        title: '手机号不正确',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    //     selectedCity: "大同市"
    // selectedDistrct: "南郊区"
    // selectedProvince
    if (this.data.addressSelect.selectedProvince == "-请选择-") {

      wx.showToast({
        title: '省市地址不能为空',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    if (!this.data.address) {
      wx.showToast({
        title: '街道地址不能为空',
        icon: 'none',
        duration: 2000
      })
      return;
    }
    console.log(this.data.city);
    debugger;
    const data = {
      consignee: this.data.consignee,
      province: this.data.addressSelect.selectedProvince,
      city: this.data.addressSelect.selectedCity,
      distrct: this.data.addressSelect.selectedDistrct,
      address: this.data.address,
      mobile: this.data.mobile,
      isDefault: this.data.isDefault
    };
    wx.cloud.callFunction({
      name: 'login',
      success: op => {
        if (op.result.openid == undefined)
          return;

        let addressArray = [];

        db.where({
            _openid: op.result.openid
          })
          .field({
            userAddress: true
          })
          .get().then(res => {
            addressArray = res.data[0].userAddress;
            if (data.isDefault) {
              addressArray.forEach(function (item, index, arr) {
                arr[index].isDefault = false;
              })
            }
            addressArray.push(data);
            if (addressArray.length == 1) {
              addressArray[0].isDefault = true;
            }
            db.where({
                _openid: op.result.openid
              })
              .update({
                data: {
                  userAddress: addressArray
                }
              });
            wx.navigateTo({
              url: '../addresses/addresses',
            })
          });
      },
      fail: res => {
        console.log(res);
      }
    });



    // console.log(data);
  },
  bindPickerChange(e) {
    this.setData({
      index: e.detail.value
    });
  }
});